cc.Class({
  extends: cc.Component,

  properties: {
    image: cc.Sprite,
  },
  initInfo(info) {
    this.info = info
    this.node.name = info.order + ""
    this.ani = []
    this.image.spriteFrame = he.load.get(`plist/${info.plist}`, cc.SpriteAtlas, 'comic').getSpriteFrame(info.pic)
    // console.log(he.load.get(`plist/${info.plist}`, cc.SpriteAtlas, 'comic'))
    this.node.opacity = 0
  },
  show() {
    if (this.info.effect) {
      he.voice.playEffect('manhua/' + this.info.effect, true)
    }

    if (this.info.music) {
      he.voice.playMusic('manhua/' + this.info.music, true)
    }

    this.tween = cc.tween(this.node)
      .to(0.5, { opacity: 255 }, { easing: 'quadIn' })
      .start()
    if (this.info.other) {
      let other = this.info.other.split("#")
      let param
      if (this.info.param) {
        param = this.info.param.split("#")
      }
      for (let i = 0; i < other.length; i++) {
        let nodeInfo
        if (param && param[i]) {
          nodeInfo = param[i].split("/")
        }
        switch (parseInt(other[i])) {
          case 1: //添加并震动
            let shakeNode = this.createSpriteNode({
              plist: nodeInfo[0],
              pic: nodeInfo[1],
              pos: cc.v2(nodeInfo[2], nodeInfo[3])
            })
            let offset = 10;
            let shakepos = cc.v2(parseInt(nodeInfo[2]), parseInt(nodeInfo[3]))
            let shakeAni = cc.tween(shakeNode)
              .to(0.1, { position: cc.v2(shakepos.x + (5 + offset), shakepos.y + (offset + 7)) })
              .to(0.1, { position: cc.v2(shakepos.x - (6 + offset), shakepos.y + (offset + 7)) })
              .to(0.1, { position: cc.v2(shakepos.x - (13 + offset), shakepos.y + (offset + 3)) })
              .to(0.1, { position: cc.v2(shakepos.x + (3 + offset), shakepos.y - (offset + 5)) })
              .to(0.1, { position: cc.v2(shakepos.x - (5 + offset), shakepos.y + (offset + 8)) })
              .to(0.1, { position: cc.v2(shakepos.x + (2 + offset), shakepos.y - (offset + 8)) })
              .to(0.1, { position: cc.v2(shakepos.x - (8 + offset), shakepos.y - (offset + 10)) })
              .to(0.1, { position: cc.v2(shakepos.x + (3 + offset), shakepos.y + (offset + 10)) })
              .to(0.1, { position: cc.v2(shakepos.x + (0 + offset), shakepos.y + (offset + 0)) })
            this.ani.push(shakeAni)
            shakeAni.start()
            break
        }
      }
    }
  },
  createSpriteNode(info) {
    let node = new cc.Node()
    let sprite = node.addComponent(cc.Sprite)
    let frame = he.load.get(`plist/${info.plist}`, cc.SpriteAtlas, 'comic').getSpriteFrame(info.pic)
    sprite.spriteFrame = frame
    node.setContentSize(frame.getOriginalSize())
    node.setPosition(info.pos)
    this.node.addChild(node)
    return node
  },
  onDestroy() {
    if (this.tween) {
      this.tween.stop()
      this.tween = null
    }
    if (this.ani & this.ani.length) {
      for (let i = this.ani.length - 1; i >= 0; i--) {
        if (this.ani[i]) {
          this.ani[i].stop()
          this.ani.splice(i, 1)
        }
      }
    }
    this.info = null
  },
});
